<?php
class IndexsBusi {
	
	/**
	 * 得到我的野猪币
	 */
	public function getUserGold($condition) {
		global $mysqlTools;
		
		 $userGold= $mysqlTools->selectSql("
	            SELECT
	            su.userGold
	            FROM
	            sys_user su where
	            su.userId = $condition
	            ");
			return ($userGold[0]);
	}
	public function queryIncomeInfos($condition) {
		global $mysqlTools;
		$condition = $mysqlTools->check_input($condition);
		$sql="
	            SELECT
	            userInFId,
	            userInFUserId,
	            userInFLoginId,
	            userInFCreatetime,
	            userInFType,
	            userInFMo,
	            roleId,
	            roleDesc,
	            userInFOthMo,
		 		userInFReason
	            FROM
	            user_bop_info  where   userInFZf=1
	           and  userInFUserId = $condition
		 		order by userInFCreatetime desc
		 		LIMIT 0,10
		 		";
		 $result= $mysqlTools->selectSql($sql);
			return $result;
	}
	
	//赠送野猪币
	public function giveGlods($params){
		global $mysqlTools;
		try {
		$mysqlTools->startTransaction();
		//增加被赠送人野猪币
		$result=$this->updateAddGlods($params['guserLoginId'],$params['userGolds'],1);
		//减少赠送人野猪币
		$result=$this->updateAddGlods($params["userLoginId"],$params['userGolds'],2);
		//被赠送人userId
		$userinfo = $this->getUserld($params['guserLoginId']);
		$userId= $userinfo['userId'];
		$glogId=getCurrentId();//被赠送人日志ID
		$logId=getCurrentId();//赠送人日志ID
		
		$ga=$this->getUserGold($userId);
		$a=$this->getUserGold($params["userId"]);
		$guserInFSurGlod=$ga['userGold']+$params['userGolds'];//被赠送人野猪币
		$userInFSurGlod=$a['userGold']-$params['userGolds'];//赠送人野猪币
		//被赠送人当时角色
		$groles=$this->getRoles($userId);
		$groleId=$groles['roleId'];
		$groleDesc=$groles['roleDesc'];
		//赠送人当时角色
		$roles=$this->getRoles($params["userId"]);
		$roleId=$roles['roleId'];
		$roleDesc=$roles['roleDesc'];
		
		//被赠送人增加日志
		$result=$this->addLogGold($glogId,1,$userId,$params['guserLoginId'],$params['userGolds'],6,$logId,$guserInFSurGlod,$groleId,$groleDesc);
		//赠送人增加日志
		$result=$this->addLogGold($logId,2,$params["userId"],$params['userLoginId'],$params['userGolds'],7,$glogId,$userInFSurGlod,$roleId,$roleDesc);
		$mysqlTools->commitTransaction();
		} catch (\Exception $e) {
    		$mysqlTools->rollback();
    	}
		return $result;
	}
	 /**
     * 修改野猪币  
     */
    public function updateAddGlods($userLoginId,$userGold,$isAdd){
    	global $mysqlTools;
    	try {
    		if(!empty($isAdd) && $isAdd==1){
    			$sql = "UPDATE `sys_user` SET `userGold` = `userGold`+".$userGold." where `userLoginId` = '".$userLoginId."'";
    		}else{
    			$sql = "UPDATE `sys_user` SET `userGold` = `userGold`-".$userGold." where `userLoginId` = '".$userLoginId."'";
    		}
    		$mysqlTools->startTransaction();
    		$result = $mysqlTools->executeSql($sql);
    		$mysqlTools->commitTransaction();
    	} catch (\Exception $e) {
    		$mysqlTools->rollback();
    	}
    	return $result;
    }
	 /**
     * 用户收支明细表日志  1得到 2失去
     */
    public function addLogGold($userInFId,$userInFZf,$userId,$userLoginId,$userInFMo,$userInFReason,$userInFReId,$userInFSurGlod,$roleId,$roleDesc){
    global $mysqlTools;
    	$sql="insert into user_bop_info(userInFId,userInFZf,userInFUserId,userInFLoginId,userInFCreatetime,userInFType,userInFMo,userInFReason,userInFReId,userInFSurGlod,roleId,roleDesc) ";
    	$sql.=" values($userInFId,$userInFZf,$userId,'$userLoginId',now(),6,$userInFMo,$userInFReason,$userInFReId,$userInFSurGlod,$roleId,'$roleDesc');";
    	$mysqlTools->startTransaction();
    	$result = $mysqlTools->executeSql($sql);
    	$mysqlTools->commitTransaction();
    	return $result;
    }
    /**
     * 通过loginId得到userId
     */
    public function getUserld($condition) {
    	global $mysqlTools;
    	$condition = $mysqlTools->check_input($condition);
    	$userId= $mysqlTools->selectSql("
    			SELECT
    			su.userId
    			FROM
    			sys_user su where
    			su.userLoginId = $condition
    			");
    	return ($userId[0]);
    }
    /**
     * 通过usernId得到角色
     */
    public function getRoles($condition) {
    	global $mysqlTools;
    	$condition = $mysqlTools->check_input($condition);
    	$rolerId= $mysqlTools->selectSql("
    			SELECT
    			sr.roleId,
    			sr.roleDesc
    			FROM
    			sys_role sr 
    			left join sys_user_role sur on sur.roleId=sr.roleId
    			where sur.userId = $condition
    			");
    	return ($rolerId[0]);
    }
    /**
     * 修改密码
     */
    public function updatemyPwd($condition){
    	global $mysqlTools;
    	//参数处理注入SQL安全问题
    	if(!empty($condition['userPwd']))
    		$pwd = $condition['userPwd'];
    	$condition = $mysqlTools->check_input($condition);
    	try {
    		$sql = "UPDATE `sys_user` SET `userId` = `userId`";
    		if(!empty($pwd)){
    			$pwd = md5($pwd);
    			$sql.= ",`userPwd` = '$pwd'";
    		}
    		$sql.=" where `userId` = $condition[userId]";
    		$mysqlTools->startTransaction();
    		$result = $mysqlTools->executeSql($sql);
    		$mysqlTools->commitTransaction();
    	} catch (\Exception $e) {
    		$mysqlTools->rollback();
    	}
    	return $result;
    }
    
}	
?>